package com.whfc.ms.api.fse.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fse.dto.FseLiftDTO;
import com.whfc.fse.dto.FseLiftFvsDeviceDTO;
import com.whfc.fse.param.*;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.fse.service.MsFseLiftService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @DESCRIPTION 升降机管理
 * @AUTHOR GuoDong_Sun
 * @DATE 2020/4/10
 */
@RequiresPermissions("fse/lift")
@RestController
@RequestMapping("/ms/api/lift")
public class FseLiftController extends BaseController {
    @Autowired
    private MsFseLiftService msFseLiftService;

    @GetMapping(value = "/list", params = {"pageNum", "pageSize"})
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "keyword", required = false) String keyword,
                       @RequestParam(value = "bindFlag", required = false) Integer bindFlag,
                       @RequestParam(value = "netState", required = false) Integer netState) {
        logger.info("升降机列表,loginer:{},pageNum:{},pageSize:{},deptId:{},keyword:{},bindFlag:{},netState:{}",
                user.getUsername(), pageNum, pageSize, deptId, keyword, bindFlag, netState);
        PageData<FseLiftDTO> pageData = msFseLiftService.list(pageNum, pageSize, deptId, keyword, bindFlag, netState);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("bindFlag") Integer bindFlag) {
        logger.info("升降机列表,loginer:{},deptId:{},bindFlag:{}", user.getUsername(), deptId, bindFlag);
        ListData<FseLiftDTO> listData = msFseLiftService.list(deptId, bindFlag);
        return ResultUtil.success(listData);
    }

    @GetMapping("/detail")
    public Result detail(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("liftId") Integer liftId) {
        logger.info("升降机详情,loginer:{},liftId:{}", user.getUsername(), liftId);
        FseLiftDTO fseLiftDTO = msFseLiftService.detail(liftId);
        return ResultUtil.success(fseLiftDTO);
    }

    @RequiresPermissions("fse/lift/device/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestBody @Validated FseLiftAddParam request) {
        logger.info("添加升降机,loginer:{},param:{}", user.getUsername(), request.toString());
        msFseLiftService.add(request);
        return ResultUtil.success();
    }

    @RequiresPermissions("fse/lift/device/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestBody @Validated FseLiftEditParam request) {
        logger.info("编辑升降机,loginer:{},param:{}", user.getUsername(), request.toString());
        msFseLiftService.edit(request);
        return ResultUtil.success();
    }

    @RequiresPermissions("fse/lift/device/del")
    @PostMapping("/del/{liftId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("liftId") Integer liftId) {
        logger.info("删除升降机,loginer:{},liftId:{}", user.getUsername(), liftId);
        msFseLiftService.del(liftId);
        return ResultUtil.success();
    }

    @PostMapping("/bind")
    public Result bind(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestBody @Validated FseLiftBindParam request) {
        logger.info("升降机绑定硬件,loginer:{},param:{}", user.getUsername(), request.toString());
        msFseLiftService.bind(request);
        return ResultUtil.success();
    }

    @PostMapping("/unbind")
    public Result unbind(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestBody @Validated FseLiftUnbindParam param) {
        logger.info("升降机解绑硬件，loginer:{},param:{}", user.getUsername(), param);
        msFseLiftService.unbind(param);
        return ResultUtil.success();
    }

    @PostMapping("/bind/fvs")
    public Result bindFvs(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestBody @Validated FseLiftFvsParam request) {
        logger.info("升降机绑定监控设备，loginer:{},request:{}", user.getUsername(), request.toString());
        msFseLiftService.bindFvs(request);
        return ResultUtil.success();
    }

    @GetMapping("/fvs/list")
    public Result fvsList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("liftId") Integer liftId) {
        logger.info("获取视频监控列表，loginer:{},liftId:{}", user.getUsername(), liftId);
        List<FseLiftFvsDeviceDTO> fvsList = msFseLiftService.fvsList(liftId);
        return ResultUtil.success(new ListData<>(fvsList));
    }

}
